package com.haiwee.springbootthymeleaf.controll;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @Auther: lichufeng
 * @Date: 2018/12/14 13:40
 * @Description:
 * 用来映射HTTP请求和页面的跳转
 * 采取了两种方式
 */
@Controller
@RequestMapping
public class ThymeleafController {


    /**
     *
     * 功能描述: 第一种跳转页面方式
     *
     */
    @GetMapping("/index1")
    public ModelAndView index(){

        ModelAndView view = new ModelAndView();
        // 设置跳转的视图 默认映射到 src/main/resources/templates/{viewName}.html
        view.setViewName("index");

        view.addObject("title","my first html");
        view.addObject("desc","welcome to 林石功 BLOG");

        Author author = new Author();
        author.setAge(18);
        author.setName("haiwee");
        author.setEmail("123@haiwee.com");

        view.addObject("author",author);

        return view;

    }

    /**
     *
     * 功能描述: 第二种跳转页面方式
     *
     * @date: 2018/12/14 2:16 PM
     */
    @GetMapping("/index2")
    public String index2(HttpServletRequest request){

        request.setAttribute("title","my first html");
        request.setAttribute("desc","welcome to 林石功 BLOG");

        Author author = new Author();
        author.setAge(18);
        author.setName("haiwee");
        author.setEmail("123@haiwee.com");

        request.setAttribute("author",author);

        // 设置跳转的视图 默认映射到以返回字符串起名的文件  src/main/resources/templates/index.html
        return "index";
    }





    class Author{

        private int age;
        private String name;
        private String email;

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }
    }
}
